<template><div><h2 id="单一入口" tabindex="-1"><a class="header-anchor" href="#单一入口"><span>单一入口</span></a></h2>
<p>Yii 中文文档 /</p>
<h2 id="单一入口-1" tabindex="-1"><a class="header-anchor" href="#单一入口-1"><span>单一入口</span></a></h2>
<p>Yii 是一个单一入口设计的框架，入口脚本是应用启动流程中的第一环，一个应用（不管是Web、控制台或 API 服务）只有一个入口脚本，用户的请求通过入口脚本实例化应用并将请求转发到应用。</p>
<p>Web 应用的入口脚本必须放在终端用户能够访问的目录下，通常命名为 <code v-pre>index.php</code>，也可以使用 Web 服务器能定位到的其他名称。</p>
<p>控制台应用的入口脚本一般在应用根目录下命名为 <code v-pre>yii</code>（yii.bat），该文件需要有执行权限，这样用户就能通过命令 <code v-pre>./yii &lt;route&gt; [arguments] [options]</code> 来运行控制台应用，例如：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php yii <span class="token builtin class-name">help</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>入口脚本主要完成以下工作：</p>
<ul>
<li>定义全局常量</li>
<li>注册 <a href="http://getcomposer.org/doc/01-basic-usage.md#autoloading" target="_blank" rel="noopener noreferrer">Composer 自动加载器</a></li>
<li>包含 <code v-pre>Yii</code> 类文件</li>
<li>加载应用配置</li>
<li>创建一个 <a href="https://learnku.com/docs/yii-framework/2.0.x/application-subject/12000" target="_blank" rel="noopener noreferrer">应用</a> 实例并配置</li>
<li>调用 [[yii\base\Application::run()]] 来处理请求</li>
</ul>
<h2 id="web-应用" tabindex="-1"><a class="header-anchor" href="#web-应用"><span>Web 应用</span></a></h2>
<p>以下是 <a href="https://learnku.com/docs/yii-framework/2.0.x/structure/11955#e1e299" target="_blank" rel="noopener noreferrer">基础应用模版</a> 入口脚本的代码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token function">defined</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'YII_DEBUG'</span><span class="token punctuation">)</span> <span class="token keyword">or</span> <span class="token function">define</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'YII_DEBUG'</span><span class="token punctuation">,</span> <span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">defined</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'YII_ENV'</span><span class="token punctuation">)</span> <span class="token keyword">or</span> <span class="token function">define</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'YII_ENV'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'dev'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 注册 Composer 自动加载器</span></span>
<span class="line"><span class="token keyword">require</span> <span class="token constant">__DIR__</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'/../vendor/autoload.php'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 包含 Yii 类文件</span></span>
<span class="line"><span class="token keyword">require</span> <span class="token constant">__DIR__</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'/../vendor/yiisoft/yii2/Yii.php'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 加载应用配置</span></span>
<span class="line"><span class="token variable">$config</span> <span class="token operator">=</span> <span class="token keyword">require</span> <span class="token constant">__DIR__</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'/../config/web.php'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 创建、配置、运行一个应用</span></span>
<span class="line"><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name class-name-fully-qualified">yii<span class="token punctuation">\</span>web<span class="token punctuation">\</span>Application</span><span class="token punctuation">(</span><span class="token variable">$config</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="控制台应用" tabindex="-1"><a class="header-anchor" href="#控制台应用"><span>控制台应用</span></a></h2>
<p>以下是一个控制台应用的入口脚本：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">#!/usr/bin/env php</span>
<span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Yii console bootstrap file.</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@link</span> https://www.yiiframework.com/</span>
<span class="line"> * <span class="token keyword">@copyright</span> Copyright (c) 2008 Yii Software LLC</span>
<span class="line"> * <span class="token keyword">@license</span> https://www.yiiframework.com/license/</span>
<span class="line"> */</span></span>
<span class="line"></span>
<span class="line"><span class="token function">defined</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'YII_DEBUG'</span><span class="token punctuation">)</span> <span class="token keyword">or</span> <span class="token function">define</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'YII_DEBUG'</span><span class="token punctuation">,</span> <span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">defined</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'YII_ENV'</span><span class="token punctuation">)</span> <span class="token keyword">or</span> <span class="token function">define</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'YII_ENV'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'dev'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 注册 Composer 自动加载器</span></span>
<span class="line"><span class="token keyword">require</span> <span class="token constant">__DIR__</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'/vendor/autoload.php'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 包含 Yii 类文件</span></span>
<span class="line"><span class="token keyword">require</span> <span class="token constant">__DIR__</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'/vendor/yiisoft/yii2/Yii.php'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 加载应用配置</span></span>
<span class="line"><span class="token variable">$config</span> <span class="token operator">=</span> <span class="token keyword">require</span> <span class="token constant">__DIR__</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'/config/console.php'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$application</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name class-name-fully-qualified">yii<span class="token punctuation">\</span>console<span class="token punctuation">\</span>Application</span><span class="token punctuation">(</span><span class="token variable">$config</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$exitCode</span> <span class="token operator">=</span> <span class="token variable">$application</span><span class="token operator">-></span><span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">exit</span><span class="token punctuation">(</span><span class="token variable">$exitCode</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="定义常量" tabindex="-1"><a class="header-anchor" href="#定义常量"><span>定义常量</span></a></h2>
<p>入口脚本是定义全局常量的最好地方，Yii 支持以下三个常量：</p>
<ul>
<li><code v-pre>YII_DEBUG</code>：标识应用是否运行在调试模式。当在调试模式下，应用会保留更多日志信息， 如果抛出异常，会显示详细的错误调用堆栈。 因此，调试模式主要适合在开发阶段使用，<code v-pre>YII_DEBUG</code> 默认值为 false。</li>
<li><code v-pre>YII_ENV</code>：标识应用运行的环境，详情请查阅 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-configurations.md#environment-constants" target="_blank" rel="noopener noreferrer">配置</a> 章节。 <code v-pre>YII_ENV</code> 默认值为 <code v-pre>'prod'</code>，表示应用运行在线上产品环境。</li>
<li><code v-pre>YII_ENABLE_ERROR_HANDLER</code>：标识是否启用 Yii 提供的错误处理， 默认为 true。</li>
</ul>
<p>当定义一个常量时，通常使用类似如下代码来定义：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">defined</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'YII_DEBUG'</span><span class="token punctuation">)</span> <span class="token keyword">or</span> <span class="token function">define</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'YII_DEBUG'</span><span class="token punctuation">,</span> <span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>上面的代码等同于：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token function">defined</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'YII_DEBUG'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token function">define</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'YII_DEBUG'</span><span class="token punctuation">,</span> <span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>显然第一段代码更加简洁易懂。</p>
<p>常量定义应该在入口脚本的开头，这样包含其他 PHP 文件时， 常量就能生效。</p>
<blockquote>
<p>💖喜欢本文档的，欢迎点赞、收藏、留言或转发，谢谢支持！<br>
作者邮箱：zhuzixian520@126.com，github地址：<a href="https://github.com/zhuzixian520" target="_blank" rel="noopener noreferrer">github.com/zhuzixian520</a></p>
</blockquote>
</div></template>


